// 统一封装接口方法
import axios from '@/utils/request'
// 引入封装的token获取方法
import { getToken } from '@/utils/token'
// 引入router
import router from '@/router/index'
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  if (getToken()?.length > 0 && config.headers.Authorization === undefined) {
    config.headers.Authorization = `Bearer ${getToken()}`
  }
  return config
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error)
})

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  return response
}, function (error) {
  // 对响应错误做点什么
  if (error.response.status === 401) {
    router.replace({ path: '/login' })
  }
  return Promise.reject(error)
})
// 获取用户选择的频道（未登录返回默认的）
const getUserChannels = () => { return axios({ url: '/v1_0/user/channels', method: 'GET' }) }
// 频道-获取所有频道
const getAllChannels = () => { return axios({ url: '/v1_0/channels', method: 'GET' }) }
// 登录请求
const login = (data) => { return axios({ url: '/v1_0/authorizations', method: 'POST', data: data }) }
// 获取文章列表
const getAllArticles = (data) => { return axios({ url: '/v1_0/articles', method: 'GET', params: { channel_id: data.channel_id, timestamp: data.timestamp } }) }
// 文章-反馈-不感兴趣
const dislikeArticle = (data) => { return axios({ url: '/v1_0/article/dislikes', method: 'POST', data: { target: data } }) }
// 文章-反馈-举报
const reportArticle = ({ artId, type }) => { return axios({ url: '/v1_0/article/reports', method: 'POST', data: { target: artId, type: type, remark: '不用写' } }) }
// 更新用户关注的频道
const updateChannels = (data) => { return axios({ url: '/v1_0/user/channels', method: 'PUT', data: data }) }
// 获取搜索推荐
const getSearchAdvice = (data) => { return axios({ url: '/v1_0/suggestion', method: 'GET', params: { q: data } }) }
// 获取搜索结果
const getSearchResult = (q) => { return axios({ url: 'v1_0/search', method: 'GET', params: { page: 1, per_page: 10, q: q } }) }
// 获取新闻详情
const getArticleDetail = (id) => { return axios({ url: `/v1_0/articles/${id}`, method: 'GET' }) }
// 用户-关注
const followAuthor = (data) => { return axios({ url: 'v1_0/user/followings', method: 'POST', data: { target: data } }) }
// 用户-取消关注
const disfollowAuthor = (data) => { return axios({ url: `v1_0/user/followings/${data}`, method: 'DELETE' }) }
// 用户-点赞
const fabulousArticle = (data) => { return axios({ url: 'v1_0/article/likings', method: 'POST', data: { target: data } }) }
// 用户-取消点赞
const disfabulousArticle = (data) => { return axios({ url: `v1_0/article/likings/${data}`, method: 'DELETE' }) }
// 获取评论
const getComments = (source, offset) => { if (!offset) { offset = null } return axios({ url: '/v1_0/comments', method: 'GET', params: { type: 'a', source: source, offset, limit: 10 } }) }
// 文章评论-喜欢
const commentLiking = (com_id) => { return axios({ url: '/v1_0/comment/likings', method: 'POST', data: { target: com_id } }) }
// 文章评论-取消喜欢
const commentDisLiking = (com_id) => { return axios({ url: `/v1_0/comment/likings/${com_id}`, method: 'DELETE' }) }
// 文章-评论
const commentSend = (id, content, art_id) => {
  let data = {}
  if (art_id === null) { data = { target: id, content: content } } else {
    data = { target: id, content: content, art_id: art_id }
  }
  return axios({ url: '/v1_0/comments', method: 'POST', data: data })
}
// 用户-获取用户个人数据
const userInfo = () => { return axios({ url: '/v1_0/user/profile', method: 'GET' }) }
// 用户-获取基本信息
const user = () => { return axios({ url: '/v1_0/user', method: 'GET' }) }
// 用户-修改资料-修改头像
const replaceHeadIMG = (data) => { return axios({ url: '/v1_0/user/photo', method: 'PATCH', data: data }) }
export { replaceHeadIMG, user, userInfo, commentSend, commentDisLiking, commentLiking, getComments, disfabulousArticle, fabulousArticle, disfollowAuthor, followAuthor, getArticleDetail, getSearchResult, getSearchAdvice, updateChannels, getAllChannels, reportArticle, dislikeArticle, getAllArticles, getUserChannels, login }
